//
//  Edit_BusinessCardViewController.m
//  fxxiaoke
//
//  Created by 鞠超 on 16/9/24.
//  Copyright © 2016年 jc. All rights reserved.
//

#import "Edit_BusinessCardViewController.h"

#import "VPImageCropperViewController.h"
#define ORIGINAL_MAX_WIDTH 640.0f

@interface Edit_BusinessCardViewController () <UIScrollViewDelegate, UIActionSheetDelegate, VPImageCropperDelegate ,UINavigationControllerDelegate ,UIImagePickerControllerDelegate, UITextFieldDelegate, UITextViewDelegate>

@property (nonatomic, strong) UIScrollView * scrollView;

@property (nonatomic, strong) UIImageView * image_Pic;
//
@property (nonatomic, strong) UITextField * text_name;
@property (nonatomic, strong) UITextField * text_tel;
@property (nonatomic, strong) UITextField * text_jobTitle;
@property (nonatomic, strong) UITextField * text_mail;
@property (nonatomic, strong) UITextField * text_company;
@property (nonatomic, strong) UITextView * text_address;
@property (nonatomic, strong) UITextView * text_label;
//占位符
@property (nonatomic, strong) UILabel * placehold_address;
@property (nonatomic, strong) UILabel * placehold_label;
//键盘高度
@property (nonatomic, assign) CGFloat keyboard_Height;

@end

@implementation Edit_BusinessCardViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self p_navi];
    
    [self p_setupView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - navi
- (void )p_navi
{
    _btnLeft.hidden = YES;
    _lblLeft.hidden = YES;
    _btnRight.hidden = YES;
    _lblRight.hidden = YES;
    
    _lblTitle.text = @"编辑名片";
    
    //左返回
    UIImageView * image_LeftRetrun = [[UIImageView alloc] initWithFrame:CGRectMake(11, 31.5, 25, 25)];
    image_LeftRetrun.userInteractionEnabled = YES;
    image_LeftRetrun.image = [UIImage imageNamed:@"Left-return.png"];
    [_topView addSubview:image_LeftRetrun];
    
    UILabel * lbl_return = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(image_LeftRetrun.frame) - 6, 35, 35, 15)];
    lbl_return.text = @"返回";
    lbl_return.textColor = [UIColor whiteColor];
    lbl_return.font = [UIFont systemFontOfSize:17];
    [_topView addSubview:lbl_return];
    
    UIButton * btn_return = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_return.frame = CGRectMake(10, 22, 60, 40);
    [_topView addSubview:btn_return];
    [btn_return addTarget:self action:@selector(btn_returnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    //右保存
    UIButton * btn_Edit = [UIButton buttonWithType:(UIButtonTypeCustom)];
    btn_Edit.frame = CGRectMake(SCREEN_WIDTH - 5 - 50, 20 + 7, 50, 30);
    [btn_Edit setTitle:@"保存" forState:(UIControlStateNormal)];
    [btn_Edit setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    btn_Edit.titleLabel.font = [UIFont systemFontOfSize:17];
    [_topView addSubview:btn_Edit];
    [btn_Edit addTarget:self action:@selector(btn_EditAction:) forControlEvents:(UIControlEventTouchUpInside)];
}

//左返回
- (void )btn_returnAction:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

//右保存
- (void )btn_EditAction:(UIButton *)sender
{
    NSLog(@"点击右保存,提交服务器");
}

//显示tabbar
- (void )viewWillAppear:(BOOL)animated
{
    [(AppDelegate *)[[UIApplication sharedApplication] delegate] hiddenTabBar];
}

#pragma mark - 布局
- (void )p_setupView
{
    //增加监听，当键盘出现或改变时收出消息
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64)];
    self.scrollView.backgroundColor = [UIColor whiteColor];
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.contentSize = CGSizeMake(0, 850);
    self.scrollView.delegate = self;
    [self.view addSubview:self.scrollView];
    
    //头像
    UILabel * lbl_heads = [[UILabel alloc] initWithFrame:CGRectMake(40, 30, 150, 15)];
    lbl_heads.text = @"头像";
    lbl_heads.textColor = [UIColor blackColor];
    lbl_heads.font = [UIFont systemFontOfSize:17.0f];
    [self.scrollView addSubview:lbl_heads];
    
    UIView * view_imageBG = [[UIView alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(lbl_heads.frame) + 10, 120, 120)];
    view_imageBG.backgroundColor = [UIColor whiteColor];
    view_imageBG.layer.cornerRadius = 5;
    view_imageBG.layer.borderColor = [UIColor groupTableViewBackgroundColor].CGColor;
    view_imageBG.layer.borderWidth = 1;
    [self.scrollView addSubview:view_imageBG];
    
    self.image_Pic = [[UIImageView alloc] initWithFrame:CGRectMake(25, CGRectGetMaxY(lbl_heads.frame) + 15, 110, 110)];
    self.image_Pic.image = [UIImage imageNamed:@"heads.jpg"];
    self.image_Pic.userInteractionEnabled = YES;
    [self.scrollView addSubview:self.image_Pic];
    
    UIButton * btn_image = [UIButton buttonWithType:(UIButtonTypeCustom)];
    btn_image.frame = CGRectMake(20, CGRectGetMaxY(lbl_heads.frame) + 10, 120, 120);
    [self.scrollView addSubview:btn_image];
    [btn_image addTarget:self action:@selector(btn_imageAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    //姓名
    UILabel * lbl_nameStar = [[UILabel alloc] initWithFrame:CGRectMake(30, CGRectGetMaxY(view_imageBG.frame) + 25, 10, 10)];
    lbl_nameStar.text = @"★";
    lbl_nameStar.textColor = [UIColor redColor];
    lbl_nameStar.font = [UIFont systemFontOfSize:8.0f];
    [self.scrollView addSubview:lbl_nameStar];
    
    UILabel * lbl_name = [[UILabel alloc] initWithFrame:CGRectMake(40, CGRectGetMaxY(view_imageBG.frame) + 25, 150, 15)];
    lbl_name.text = @"姓名";
    lbl_name.textColor = [UIColor blackColor];
    lbl_name.font = [UIFont systemFontOfSize:17.0f];
    [self.scrollView addSubview:lbl_name];
    
    UIView * view_name = [[UIView alloc] initWithFrame:CGRectMake(25, CGRectGetMaxY(lbl_name.frame) + 10, SCREEN_WIDTH - 50, 40)];
    view_name.layer.cornerRadius = 5;
    view_name.layer.borderWidth = 1;
    view_name.layer.borderColor = [UIColor groupTableViewBackgroundColor].CGColor;
    [self.scrollView addSubview:view_name];
    
    self.text_name = [[UITextField alloc] initWithFrame:CGRectMake(15, 7.5, SCREEN_WIDTH - 80, 25)];
    self.text_name.textColor = [UIColor grayColor];
    self.text_name.text = @"姓名";
    self.text_name.delegate = self;
    self.text_name.returnKeyType = UIReturnKeyDone;
    [view_name addSubview:self.text_name];
    
    //电话
    UILabel * lbl_telStar = [[UILabel alloc] initWithFrame:CGRectMake(30, CGRectGetMaxY(view_name.frame) + 20, 10, 10)];
    lbl_telStar.text = @"★";
    lbl_telStar.textColor = [UIColor redColor];
    lbl_telStar.font = [UIFont systemFontOfSize:8.0f];
    [self.scrollView addSubview:lbl_telStar];
    
    UILabel * lbl_tel = [[UILabel alloc] initWithFrame:CGRectMake(40, CGRectGetMaxY(view_name.frame) + 20, 150, 15)];
    lbl_tel.text = @"电话";
    lbl_tel.textColor = [UIColor blackColor];
    lbl_tel.font = [UIFont systemFontOfSize:17.0f];
    [self.scrollView addSubview:lbl_tel];
    
    UIView * view_tel = [[UIView alloc] initWithFrame:CGRectMake(25, CGRectGetMaxY(lbl_tel.frame) + 10, SCREEN_WIDTH - 50, 40)];
    view_tel.layer.cornerRadius = 5;
    view_tel.layer.borderWidth = 1;
    view_tel.layer.borderColor = [UIColor groupTableViewBackgroundColor].CGColor;
    [self.scrollView addSubview:view_tel];
    
    self.text_tel = [[UITextField alloc] initWithFrame:CGRectMake(15, 7.5, SCREEN_WIDTH - 80, 25)];
    self.text_tel.textColor = [UIColor grayColor];
    self.text_tel.text = @"1866999XXXX";
    self.text_tel.delegate = self;
    self.text_tel.returnKeyType = UIReturnKeyDone;
    self.text_tel.keyboardType = UIKeyboardTypeNumberPad;
    [view_tel addSubview:self.text_tel];
    
    
    //职位
    UILabel * lbl_jobTitle = [[UILabel alloc] initWithFrame:CGRectMake(40, CGRectGetMaxY(view_tel.frame) + 20, 150, 15)];
    lbl_jobTitle.text = @"职位";
    lbl_jobTitle.textColor = [UIColor blackColor];
    lbl_jobTitle.font = [UIFont systemFontOfSize:17.0f];
    [self.scrollView addSubview:lbl_jobTitle];
    
    UIView * view_jobTitle = [[UIView alloc] initWithFrame:CGRectMake(25, CGRectGetMaxY(lbl_jobTitle.frame) + 10, SCREEN_WIDTH - 50, 40)];
    view_jobTitle.layer.cornerRadius = 5;
    view_jobTitle.layer.borderWidth = 1;
    view_jobTitle.layer.borderColor = [UIColor groupTableViewBackgroundColor].CGColor;
    [self.scrollView addSubview:view_jobTitle];
    
    self.text_jobTitle = [[UITextField alloc] initWithFrame:CGRectMake(15, 7.5, SCREEN_WIDTH - 80, 25)];
    self.text_jobTitle.textColor = [UIColor grayColor];
    self.text_jobTitle.text = @"职位名称";
    self.text_jobTitle.delegate = self;
    self.text_jobTitle.returnKeyType = UIReturnKeyDone;
    [view_jobTitle addSubview:self.text_jobTitle];
    
    
    //邮箱地址
    UILabel * lbl_mail = [[UILabel alloc] initWithFrame:CGRectMake(40, CGRectGetMaxY(view_jobTitle.frame) + 20, 150, 15)];
    lbl_mail.text = @"邮箱地址";
    lbl_mail.textColor = [UIColor blackColor];
    lbl_mail.font = [UIFont systemFontOfSize:17.0f];
    [self.scrollView addSubview:lbl_mail];
    
    UIView * view_mail = [[UIView alloc] initWithFrame:CGRectMake(25, CGRectGetMaxY(lbl_mail.frame) + 10, SCREEN_WIDTH - 50, 40)];
    view_mail.layer.cornerRadius = 5;
    view_mail.layer.borderWidth = 1;
    view_mail.layer.borderColor = [UIColor groupTableViewBackgroundColor].CGColor;
    [self.scrollView addSubview:view_mail];
    
    self.text_mail = [[UITextField alloc] initWithFrame:CGRectMake(15, 7.5, SCREEN_WIDTH - 80, 25)];
    self.text_mail.textColor = [UIColor grayColor];
    self.text_mail.text = @"qwertyuiop.zhishun.net";
    self.text_mail.delegate = self;
    self.text_mail.returnKeyType = UIReturnKeyDone;
    [view_mail addSubview:self.text_mail];
    
    
    //公司
    UILabel * lbl_companyStar = [[UILabel alloc] initWithFrame:CGRectMake(30, CGRectGetMaxY(view_mail.frame) + 20, 10, 10)];
    lbl_companyStar.text = @"★";
    lbl_companyStar.textColor = [UIColor redColor];
    lbl_companyStar.font = [UIFont systemFontOfSize:8.0f];
    [self.scrollView addSubview:lbl_companyStar];
    
    UILabel * lbl_company = [[UILabel alloc] initWithFrame:CGRectMake(40, CGRectGetMaxY(view_mail.frame) + 20, 150, 15)];
    lbl_company.text = @"公司";
    lbl_company.textColor = [UIColor blackColor];
    lbl_company.font = [UIFont systemFontOfSize:17.0f];
    [self.scrollView addSubview:lbl_company];
    
    UIView * view_company = [[UIView alloc] initWithFrame:CGRectMake(25, CGRectGetMaxY(lbl_company.frame) + 10, SCREEN_WIDTH - 50, 40)];
    view_company.layer.cornerRadius = 5;
    view_company.layer.borderWidth = 1;
    view_company.layer.borderColor = [UIColor groupTableViewBackgroundColor].CGColor;
    [self.scrollView addSubview:view_company];
    
    self.text_company = [[UITextField alloc] initWithFrame:CGRectMake(15, 7.5, SCREEN_WIDTH - 80, 25)];
    self.text_company.textColor = [UIColor grayColor];
    self.text_company.text = @"公司名称";
    self.text_company.delegate = self;
    self.text_company.returnKeyType = UIReturnKeyDone;
    [view_company addSubview:self.text_company];
    
    
    //公司地址
    UILabel * lbl_address = [[UILabel alloc] initWithFrame:CGRectMake(40, CGRectGetMaxY(view_company.frame) + 20, 150, 15)];
    lbl_address.text = @"地址";
    lbl_address.textColor = [UIColor blackColor];
    lbl_address.font = [UIFont systemFontOfSize:17.0f];
    [self.scrollView addSubview:lbl_address];
    
    UIView * view_address = [[UIView alloc] initWithFrame:CGRectMake(25, CGRectGetMaxY(lbl_address.frame) + 10, SCREEN_WIDTH - 50, 65)];
    view_address.layer.cornerRadius = 5;
    view_address.layer.borderWidth = 1;
    view_address.layer.borderColor = [UIColor groupTableViewBackgroundColor].CGColor;
    [self.scrollView addSubview:view_address];
    
    self.text_address = [[UITextView alloc] initWithFrame:CGRectMake(10, 5, SCREEN_WIDTH - 70, 55)];
    self.text_address.textColor = [UIColor grayColor];
    self.text_address.font = [UIFont systemFontOfSize:17.0f];
    self.text_address.delegate = self;
    [view_address addSubview:self.text_address];
    
    self.placehold_address = [[UILabel alloc] initWithFrame:CGRectMake(5, 8, 100, 20)];
    self.placehold_address.text = @"未填写";
    self.placehold_address.font = [UIFont systemFontOfSize:17.0f];
    self.placehold_address.textColor = [UIColor grayColor];
    [self.text_address addSubview:self.placehold_address];
    
    //签名
    UILabel * lbl_label = [[UILabel alloc] initWithFrame:CGRectMake(40, CGRectGetMaxY(view_address.frame) + 20, 150, 15)];
    lbl_label.text = @"签名";
    lbl_label.textColor = [UIColor blackColor];
    lbl_label.font = [UIFont systemFontOfSize:17.0f];
    [self.scrollView addSubview:lbl_label];
    
    UIView * view_label = [[UIView alloc] initWithFrame:CGRectMake(25, CGRectGetMaxY(lbl_label.frame) + 10, SCREEN_WIDTH - 50, 65)];
    view_label.layer.cornerRadius = 5;
    view_label.layer.borderWidth = 1;
    view_label.layer.borderColor = [UIColor groupTableViewBackgroundColor].CGColor;
    [self.scrollView addSubview:view_label];
    
    self.text_label = [[UITextView alloc] initWithFrame:CGRectMake(10, 5, SCREEN_WIDTH - 70, 55)];
    self.text_label.textColor = [UIColor grayColor];
    self.text_label.font = [UIFont systemFontOfSize:17.0f];
    self.text_label.delegate = self;
    [view_label addSubview:self.text_label];
    
    self.placehold_label = [[UILabel alloc] initWithFrame:CGRectMake(5, 8, 200, 20)];
    self.placehold_label.text = @"请精简展示品牌特性";
    self.placehold_label.font = [UIFont systemFontOfSize:17.0f];
    self.placehold_label.textColor = [UIColor grayColor];
    [self.text_label addSubview:self.placehold_label];
}

#pragma mark - textfiled代理
- (BOOL )textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    
    return YES;
}

#pragma mark - scrollView代理
- (void )scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.text_name resignFirstResponder];
    [self.text_tel resignFirstResponder];
    [self.text_jobTitle resignFirstResponder];
    [self.text_mail resignFirstResponder];
    [self.text_company resignFirstResponder];
    [self.text_address resignFirstResponder];
    [self.text_label resignFirstResponder];
}

#pragma mark - textView代理
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    if([textView isEqual:self.text_address])
    {
        self.placehold_address.hidden = YES;
    }
    else if([textView isEqual:self.text_label])
    {
        self.placehold_label.hidden = YES;
    }
}

- (void )textViewDidEndEditing:(UITextView *)textView
{
    if([textView isEqual:self.text_address])
    {
        if([textView.text length] == 0)
        {
            self.placehold_address.hidden = NO;
        }
    }
    else if([textView isEqual:self.text_label])
    {
        if([textView.text length] == 0)
        {
            self.placehold_label.hidden = NO;
        }
    }
}

#pragma mark - 键盘高度
- (void)keyboardWillShow:(NSNotification *)aNotification
{
    NSDictionary *userInfo = [aNotification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    self.keyboard_Height = keyboardRect.size.height;
//    NSLog(@"%lf",self.keyboard_Height);
    
    
    if([self.text_name isFirstResponder])
    {
        if(self.scrollView.contentOffset.y < 275 - (SCREEN_HEIGHT - 64 - self.keyboard_Height))
        {
            [UIView animateWithDuration:0.7f animations:^{
                
                self.scrollView.contentOffset = CGPointMake(0, 275 - (SCREEN_HEIGHT - 64 - self.keyboard_Height));
            }];
        }
    }
    else if([self.text_tel isFirstResponder])
    {
        if(self.scrollView.contentOffset.y < 360 - (SCREEN_HEIGHT - 64 - self.keyboard_Height))
        {
            [UIView animateWithDuration:0.7f animations:^{
                
                self.scrollView.contentOffset = CGPointMake(0, 360 - (SCREEN_HEIGHT - 64 - self.keyboard_Height));
            }];
        }
    }
    else if([self.text_jobTitle isFirstResponder])
    {
        if(self.scrollView.contentOffset.y < 445 - (SCREEN_HEIGHT - 64 - self.keyboard_Height))
        {
            [UIView animateWithDuration:0.7f animations:^{
                
                self.scrollView.contentOffset = CGPointMake(0, 445 - (SCREEN_HEIGHT - 64 - self.keyboard_Height));
            }];
        }
    }
    else if([self.text_mail isFirstResponder])
    {
        if(self.scrollView.contentOffset.y < 530 - (SCREEN_HEIGHT - 64 - self.keyboard_Height))
        {
            [UIView animateWithDuration:0.7f animations:^{
                
                self.scrollView.contentOffset = CGPointMake(0, 530 - (SCREEN_HEIGHT - 64 - self.keyboard_Height));
            }];
        }
    }
    else if([self.text_company isFirstResponder])
    {
        if(self.scrollView.contentOffset.y < 615 - (SCREEN_HEIGHT - 64 - self.keyboard_Height))
        {
            [UIView animateWithDuration:0.7f animations:^{
                
                self.scrollView.contentOffset = CGPointMake(0, 615 - (SCREEN_HEIGHT - 64 - self.keyboard_Height));
            }];
        }
    }
    else if([self.text_address isFirstResponder])
    {
        if(self.scrollView.contentOffset.y < 725 - (SCREEN_HEIGHT - 64 - self.keyboard_Height))
        {
            [UIView animateWithDuration:0.7f animations:^{
                
                self.scrollView.contentOffset = CGPointMake(0, 725 - (SCREEN_HEIGHT - 64 - self.keyboard_Height));
            }];
        }
    }
    else if([self.text_label isFirstResponder])
    {
        if(self.scrollView.contentOffset.y < 835 - (SCREEN_HEIGHT - 64 - self.keyboard_Height))
        {
            [UIView animateWithDuration:0.7f animations:^{
                
                self.scrollView.contentOffset = CGPointMake(0, 835 - (SCREEN_HEIGHT - 64 - self.keyboard_Height));
            }];
        }
    }
}









































//****************************处理图片********************************
#pragma mark - 点击头像
- (void )btn_imageAction:(UIButton *)sender
{//NSLog(@"点击头像,拍照或调取相册");
    [self.text_name resignFirstResponder];
    [self.text_tel resignFirstResponder];
    [self.text_jobTitle resignFirstResponder];
    [self.text_mail resignFirstResponder];
    [self.text_company resignFirstResponder];
    [self.text_address resignFirstResponder];
    [self.text_label resignFirstResponder];
    
    UIActionSheet * choiceSheet = [[UIActionSheet alloc] initWithTitle:@"请选择获取方式" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"本地相册", @"照相机", nil];
    [choiceSheet showInView:self.view];
}

#pragma mark VPImageCropperDelegate
- (void)imageCropper:(VPImageCropperViewController *)cropperViewController didFinished:(UIImage *)editedImage
{
    [cropperViewController dismissViewControllerAnimated:YES completion:^{
        // TO DO
        [self saveImage:editedImage withName:@"avatar.jpg"];
        //加数据(接口需要的)
        //        NSData *imageData = UIImagePNGRepresentation(editedImage);
        
        self.image_Pic.image = editedImage;
    }];
}

#pragma mark - 保存图片至沙盒
- (void )saveImage:(UIImage *)currentImage withName:(NSString *)imageName
{
    NSData *imageData = UIImageJPEGRepresentation(currentImage, 0.5);
    // 获取沙盒目录
    NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName];
    // 将图片写入文件
    [imageData writeToFile:fullPath atomically:NO];
}

- (void)imageCropperDidCancel:(VPImageCropperViewController *)cropperViewController
{
    [cropperViewController dismissViewControllerAnimated:YES completion:^{
        
    }];
}

#pragma mark UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1)
    {// 拍照
        if ([self isCameraAvailable] && [self doesCameraSupportTakingPhotos])
        {
            UIImagePickerController *controller = [[UIImagePickerController alloc] init];
            controller.sourceType = UIImagePickerControllerSourceTypeCamera;
            if ([self isFrontCameraAvailable]) {
                controller.cameraDevice = UIImagePickerControllerCameraDeviceRear;
            }
            NSMutableArray *mediaTypes = [[NSMutableArray alloc] init];
            [mediaTypes addObject:(__bridge NSString *)kUTTypeImage];
            controller.mediaTypes = mediaTypes;
            controller.delegate = self;
            
            [self presentViewController:controller
                               animated:YES
                             completion:^(void){
                             }];
        }
    }
    else if (buttonIndex == 0)
    {// 从相册中选取
        if ([self isPhotoLibraryAvailable])
        {
            UIImagePickerController *controller = [[UIImagePickerController alloc] init];
            controller.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            NSMutableArray *mediaTypes = [[NSMutableArray alloc] init];
            [mediaTypes addObject:(__bridge NSString *)kUTTypeImage];
            controller.mediaTypes = mediaTypes;
            controller.delegate = self;
            
            [self presentViewController:controller
                               animated:YES
                             completion:^(void){
                             }];
        }
    }
}

#pragma mark - UIImagePickerControllerDelegate
//触发事件
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:^() {
        UIImage *portraitImg = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        portraitImg = [self imageByScalingToMaxSize:portraitImg];
        // 裁剪
        VPImageCropperViewController *imgEditorVC = [[VPImageCropperViewController alloc] initWithImage:portraitImg cropFrame:CGRectMake(1, 100.0f, SCREEN_WIDTH - 2, SCREEN_WIDTH) limitScaleRatio:3.0];
        imgEditorVC.delegate = self;
        imgEditorVC.view.backgroundColor = [UIColor blackColor];
        [self presentViewController:imgEditorVC animated:YES completion:^{
            // TO DO
        }];
    }];
}

//取消事件
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:^(){
        
    }];
}

#pragma mark - UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    
}

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    
}

#pragma mark camera utility
- (BOOL) isCameraAvailable
{
    return [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
}

- (BOOL) isRearCameraAvailable
{
    return [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
}

- (BOOL) isFrontCameraAvailable
{
    return [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront];
}

- (BOOL) doesCameraSupportTakingPhotos
{
    return [self cameraSupportsMedia:(__bridge NSString *)kUTTypeImage sourceType:UIImagePickerControllerSourceTypeCamera];
}

- (BOOL) isPhotoLibraryAvailable
{
    return [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary];
}

- (BOOL) canUserPickVideosFromPhotoLibrary
{
    return [self
            cameraSupportsMedia:(__bridge NSString *)kUTTypeMovie sourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}

- (BOOL) canUserPickPhotosFromPhotoLibrary
{
    return [self
            cameraSupportsMedia:(__bridge NSString *)kUTTypeImage sourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}

- (BOOL) cameraSupportsMedia:(NSString *)paramMediaType sourceType:(UIImagePickerControllerSourceType)paramSourceType
{
    __block BOOL result = NO;
    if ([paramMediaType length] == 0)
    {
        return NO;
    }
    NSArray *availableMediaTypes = [UIImagePickerController availableMediaTypesForSourceType:paramSourceType];
    [availableMediaTypes enumerateObjectsUsingBlock: ^(id obj, NSUInteger idx, BOOL *stop) {
        NSString *mediaType = (NSString *)obj;
        if ([mediaType isEqualToString:paramMediaType])
        {
            result = YES;
            *stop= YES;
        }
    }];
    
    return result;
}

#pragma mark image scale utility
- (UIImage *)imageByScalingToMaxSize:(UIImage *)sourceImage
{
    if (sourceImage.size.width < ORIGINAL_MAX_WIDTH) return sourceImage;
    CGFloat btWidth = 0.0f;
    CGFloat btHeight = 0.0f;
    if (sourceImage.size.width > sourceImage.size.height)
    {
        btHeight = ORIGINAL_MAX_WIDTH;
        btWidth = sourceImage.size.width * (ORIGINAL_MAX_WIDTH / sourceImage.size.height);
    } else
    {
        btWidth = ORIGINAL_MAX_WIDTH;
        btHeight = sourceImage.size.height * (ORIGINAL_MAX_WIDTH / sourceImage.size.width);
    }
    CGSize targetSize = CGSizeMake(btWidth, btHeight);
    
    return [self imageByScalingAndCroppingForSourceImage:sourceImage targetSize:targetSize];
}

- (UIImage *)imageByScalingAndCroppingForSourceImage:(UIImage *)sourceImage targetSize:(CGSize)targetSize
{
    UIImage *newImage = nil;
    CGSize imageSize = sourceImage.size;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    CGFloat targetWidth = targetSize.width;
    CGFloat targetHeight = targetSize.height;
    CGFloat scaleFactor = 0.0;
    CGFloat scaledWidth = targetWidth;
    CGFloat scaledHeight = targetHeight;
    CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
    
    if (CGSizeEqualToSize(imageSize, targetSize) == NO)
    {
        CGFloat widthFactor = targetWidth / width;
        CGFloat heightFactor = targetHeight / height;
        
        if (widthFactor > heightFactor)
            scaleFactor = widthFactor; // scale to fit height
        else
            scaleFactor = heightFactor; // scale to fit width
        scaledWidth  = width * scaleFactor;
        scaledHeight = height * scaleFactor;
        
        // center the image
        if (widthFactor > heightFactor)
        {
            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
        }
        else if (widthFactor < heightFactor)
        {
            thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
        }
    }
    
    UIGraphicsBeginImageContext(targetSize); // this will crop
    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = thumbnailPoint;
    thumbnailRect.size.width  = scaledWidth;
    thumbnailRect.size.height = scaledHeight;
    
    [sourceImage drawInRect:thumbnailRect];
    
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    if(newImage == nil) NSLog(@"could not scale image");
    
    //pop the context to get back to the default
    UIGraphicsEndImageContext();
    
    return newImage;
}

@end
